package com.entersoft.sicep.log4j;

import java.util.*;
/**
 * Package: log4j
 * Clase: Split
 * Descripción: Control de valores.
 * Versión SICEP: 3.0
 * Autor: Cesar Pino - Entersoft
 * fecha creación: xx/xx/2012
 * ----------------------------------
 * 
 * Modificado por: XXXXXXXXXXXXXXXXXX
 * Motivo de actualización: XXXXXXXXX
 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 * fecha actualización: xx/xx/2012
 */
public final class Split implements java.io.Serializable {
	
	//private static final String DATA = "DATA [";
	//private static final String SEPARADOR = "]  SEPARADOR [";						   
							   
	private Split() 
	{
		super();
	}


	public static String[] split(String str, String sep) 
	{
		
		//final String PREFIX = "[split] ";
		
		String ret[] = null;
		try {
						
			StringTokenizer st = new StringTokenizer(str, sep);
			ret = new String[st.countTokens()];
			int r = 0;

			while (st.hasMoreTokens()) {
				ret[r++] = st.nextToken();
			}			
		}
		catch(Exception e) {
			ret = null;
			
		}


		return ret;
	}
	
		public static String[] separarParteEnteraDecimal(String str, String sep) {
		
		//final String PREFIX = "[separarParteEnteraDecimal] ";
				
		String ret[] = null;
		try {
			StringTokenizer st = new StringTokenizer(str, sep);
			ret = new String[2];
			
			if( st.hasMoreTokens() ) ret[0] = st.nextToken();
			else ret[0] = "0";
			
			if( st.hasMoreTokens() ) ret[1] = st.nextToken();
			else ret[1] = "0";			
		}
		catch(Exception e) {
			ret = null;
			
		}


		return ret;		
	
	}
	
	public static String[] SplitToken(String data, String separador){
		
		//final String PREFIX = "[SplitToken] ";
				
//		StringBuffer sb = new StringBuffer("");
		StringTokenizer st = new StringTokenizer(data, separador);
	
		String arreglo[]=new String[st.countTokens()];
//		int cntArreglo=0;

		int index = 0;
		while( st.hasMoreTokens() ){
			arreglo[ index ] = st.nextToken();
			index++;
		}


		return arreglo;
	}
	
	public static String[] splitGenerarToken(String str, String sep) 
	{
		
		//final String PREFIX = "[splitGenerarToken] ";
				
		String ret[] = null;
		try {
						
			StringTokenizer st = new StringTokenizer(str, sep);
			ret = new String[st.countTokens()];
			int r = 0;

			while (st.hasMoreTokens()) {
				ret[r++] = st.nextToken();
			}			
		}
		catch(Exception e) {
			ret = null;
			
		}


		return ret;
	}
	
	
	public static String[] SPLIT(String str, String sep) 
	{	
				
		Collection list = null;
		String ret[] = null;

		
		String tmp = str;
		
		while( tmp.indexOf( sep ) >= 0 ){

				
				if(list == null)
					list = new ArrayList();
				
				list.add( tmp.substring(0, tmp.indexOf( sep )) );
				
				tmp = tmp.substring( tmp.indexOf( sep ) +1 );


		}
		
		if( tmp != null ){
		
			if(list == null)
				list = new ArrayList();
			
			list.add( tmp ); //EL ULTIMO TOKEN YA NO POSEE SEPARADOR
		}


			ret = new String[list.size()];
			int r = 0;
			Iterator it = list.iterator();
			while (it.hasNext()) {
				ret[r++] = (String)it.next();
			}			



		return ret;
	}
	
	
	private static boolean poseeValorEntreSeparador(String data, String separador){
		boolean flag = true;
			
//		int pos = 0;


	
		String tmp = data;
		String dataAux = "";
		String tmp2 = "";
			
		while( tmp.indexOf( separador ) >= 0 ){
		
				tmp2 = tmp;		
				tmp = tmp.substring( tmp.indexOf( separador ) +1 );
				dataAux = tmp2.substring(0, tmp2.indexOf( tmp ));
				
				if( dataAux.equals( separador ) )
					return false;		
		
		}
		
		if( data.endsWith( separador ) )
			flag = false;
			
			
		return flag;	
	}
	
		
}
